package com.cxg.fairy.ec.main.personal.address;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.order.PostOrderDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.MyDividerItemDecoration;
import com.cxg.fairy.util.storage.LattePreference;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by CFairy
 */

public class AddressListDelegate extends LatteDelegate implements ISuccess {

    public static final String POST_ORDER_ADDRESS_ID = "POST_ORDER_ADDRESS_ID";

    private static PostOrderDelegate mPostOrderDelegate = null;

    private static void setPostOrderDelegate(PostOrderDelegate delegate) {
        mPostOrderDelegate = delegate;
    }

    private static PostOrderDelegate getPostOrderDelegate() {
        return mPostOrderDelegate;
    }

    @BindView(R2.id.rv_address)
    RecyclerView mRecyclerView = null;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_address_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        RestClient.builder()
                .url("getAddressByConsumerId")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    public static AddressListDelegate create(int addressId, PostOrderDelegate postOrderDelegate) {
        final Bundle args = new Bundle();
        args.putInt(POST_ORDER_ADDRESS_ID, addressId);
        setPostOrderDelegate(postOrderDelegate);
        final AddressListDelegate delegate = new AddressListDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onSuccess(String response) {
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(manager);
        final List<MultipleItemEntity> data = new AddressListDataConverter().setJsonData(response).convert();
        final AddressListAdapter addressListAdapter = new AddressListAdapter(data);


        //若客户端内存已有数据
        Object obj = LattePreference.getCustomAppProfile(POST_ORDER_ADDRESS_ID);


        if (obj != null && obj!="" && !obj.equals("")) {
            addressListAdapter.setId(Integer.parseInt(obj + ""));
        } else {
            addressListAdapter.setId(-1);
        }


        MyDividerItemDecoration itemDecoration = new MyDividerItemDecoration(this.getContext(),0,0.5f,0, Color.parseColor("#FFF8DC"));
        mRecyclerView.addItemDecoration(itemDecoration);
        mRecyclerView.setAdapter(addressListAdapter);

        mRecyclerView.addOnItemTouchListener(new AddressListClickListener(this,getPostOrderDelegate()));
    }

}
